package com.xh.fma.commodity.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.xh.fma.commodity.entity.AttrAttrgroupRelationEntity;
import com.xh.fma.commodity.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xh.fma.commodity.entity.AttrEntity;
import com.xh.fma.commodity.service.AttrService;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品属性表
 *
 * @author sjf
 * @email xhyy7762@gmail.com
 * @date 2023-05-28 11:16:29
 */
@RestController
@RequestMapping("commodity/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/base/list/{categoryId}")
    public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrService.queryPageBySelect(params,categoryId,1);
        return R.ok().put("page", page);
    }
    @GetMapping("/sale/list/{categoryId}")
    public R saleAttrList(@RequestParam Map<String, Object> params, @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrService.queryPageBySelect(params,categoryId,0);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("commodity:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:attr:save")
    public R save(@RequestBody AttrEntity attr){
		attrService.saveAttrAndRelation(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:attr:update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);
        AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
        entity.setAttrId(attr.getAttrId());
        entity.setAttrGroupId(attr.getAttrGroupId());
        attrAttrgroupRelationService.updateByAttrId(entity);
        return R.ok();
    }
    @RequestMapping("/saleupdate")
    //@RequiresPermissions("commodity:attr:update")
    public R saleUpdate(@RequestBody AttrEntity attr){
        attrService.updateById(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
		attrAttrgroupRelationService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }
    @RequestMapping("/sale/delete")
    //@RequiresPermissions("commodity:attr:delete")
    public R saleDelete(@RequestBody Long[] attrIds){
        attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
